
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.io.abinit.helpers &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script src="../../../../_static/jquery.js"></script>
    <script src="../../../../_static/underscore.js"></script>
    <script src="../../../../_static/doctools.js"></script>
    <script src="../../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.io.abinit.helpers</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.io.abinit.helpers</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>


<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Helper methods for generating gw input / and work flows.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Michiel van Setten&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span>
<span class="n">__version__</span> <span class="o">=</span> <span class="s2">&quot;0.9&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Michiel van Setten&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;mjvansetten@gmail.com&quot;</span>
<span class="n">__date__</span> <span class="o">=</span> <span class="s2">&quot;May 2014&quot;</span>

<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">ast</span>
<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">math</span>
<span class="kn">import</span> <span class="nn">shutil</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">pymatgen.core.units</span> <span class="kn">import</span> <span class="n">eV_to_Ha</span>
<span class="kn">from</span> <span class="nn">pymatgen.symmetry.analyzer</span> <span class="kn">import</span> <span class="n">SpacegroupAnalyzer</span>
<span class="kn">from</span> <span class="nn">pymatgen.transformations.standard_transformations</span> <span class="kn">import</span> <span class="n">OxidationStateRemovalTransformation</span><span class="p">,</span> \
    <span class="n">PrimitiveCellTransformation</span><span class="p">,</span> <span class="n">SupercellTransformation</span>


<div class="viewcode-block" id="now"><a class="viewcode-back" href="../../../../pymatgen.io.abinit.helpers.html#pymatgen.io.abinit.helpers.now">[docs]</a><span class="k">def</span> <span class="nf">now</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    helper to return a time string</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%H:%M:%S </span><span class="si">%d</span><span class="s2">/%m/%Y&quot;</span><span class="p">)</span></div>


<div class="viewcode-block" id="read_extra_abivars"><a class="viewcode-back" href="../../../../pymatgen.io.abinit.helpers.html#pymatgen.io.abinit.helpers.read_extra_abivars">[docs]</a><span class="k">def</span> <span class="nf">read_extra_abivars</span><span class="p">():</span>
    <span class="n">ea</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="s1">&#39;extra_abivars&#39;</span><span class="p">):</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;extra_abivars&#39;</span><span class="p">)</span>
        <span class="n">ea</span> <span class="o">=</span> <span class="n">ast</span><span class="o">.</span><span class="n">literal_eval</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">ea</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span>
    <span class="k">return</span> <span class="n">ea</span></div>


<div class="viewcode-block" id="refine_structure"><a class="viewcode-back" href="../../../../pymatgen.io.abinit.helpers.html#pymatgen.io.abinit.helpers.refine_structure">[docs]</a><span class="k">def</span> <span class="nf">refine_structure</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">symprec</span><span class="o">=</span><span class="mf">1e-3</span><span class="p">):</span>
    <span class="n">remove_ox</span> <span class="o">=</span> <span class="n">OxidationStateRemovalTransformation</span><span class="p">()</span>
    <span class="n">structure</span> <span class="o">=</span> <span class="n">remove_ox</span><span class="o">.</span><span class="n">apply_transformation</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>
    <span class="n">sym_finder</span> <span class="o">=</span> <span class="n">SpacegroupAnalyzer</span><span class="p">(</span><span class="n">structure</span><span class="o">=</span><span class="n">structure</span><span class="p">,</span> <span class="n">symprec</span><span class="o">=</span><span class="n">symprec</span><span class="p">)</span>
    <span class="n">structure</span> <span class="o">=</span> <span class="n">sym_finder</span><span class="o">.</span><span class="n">get_refined_structure</span><span class="p">()</span>
    <span class="n">get_prim</span> <span class="o">=</span> <span class="n">PrimitiveCellTransformation</span><span class="p">()</span>
    <span class="n">structure</span> <span class="o">=</span> <span class="n">get_prim</span><span class="o">.</span><span class="n">apply_transformation</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>
    <span class="n">m</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">matrix</span>
    <span class="n">x_prod</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">m</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="n">m</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
    <span class="k">if</span> <span class="n">x_prod</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">x_prod</span><span class="p">)</span>
        <span class="n">trans</span> <span class="o">=</span> <span class="n">SupercellTransformation</span><span class="p">(((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)))</span>
        <span class="n">structure</span> <span class="o">=</span> <span class="n">trans</span><span class="o">.</span><span class="n">apply_transformation</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>
        <span class="n">m</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">lattice</span><span class="o">.</span><span class="n">matrix</span>
        <span class="n">x_prod</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">(</span><span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">m</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="n">m</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">x_prod</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">x_prod</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span>
    <span class="k">return</span> <span class="n">structure</span></div>


<div class="viewcode-block" id="s_name"><a class="viewcode-back" href="../../../../pymatgen.io.abinit.helpers.html#pymatgen.io.abinit.helpers.s_name">[docs]</a><span class="k">def</span> <span class="nf">s_name</span><span class="p">(</span><span class="n">structure</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="s1">&#39;old&#39;</span><span class="p">):</span>
        <span class="n">name_</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">name_</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">composition</span><span class="o">.</span><span class="n">reduced_formula</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;_&#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">structure</span><span class="o">.</span><span class="n">item</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">name_</span></div>


<div class="viewcode-block" id="clean"><a class="viewcode-back" href="../../../../pymatgen.io.abinit.helpers.html#pymatgen.io.abinit.helpers.clean">[docs]</a><span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="n">some_string</span><span class="p">,</span> <span class="n">uppercase</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    helper to clean up an input string</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">uppercase</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">some_string</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">some_string</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span></div>


<div class="viewcode-block" id="expand"><a class="viewcode-back" href="../../../../pymatgen.io.abinit.helpers.html#pymatgen.io.abinit.helpers.expand">[docs]</a><span class="k">def</span> <span class="nf">expand</span><span class="p">(</span><span class="n">tests</span><span class="p">,</span> <span class="n">level</span><span class="p">):</span>
    <span class="kn">from</span> <span class="nn">abipy.gw.codeinterfaces</span> <span class="kn">import</span> <span class="n">get_all_ecuteps</span><span class="p">,</span> <span class="n">get_all_nbands</span>
    <span class="n">new_tests</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">tests</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">test</span> <span class="ow">in</span> <span class="n">tests</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
        <span class="k">if</span> <span class="n">test</span> <span class="ow">in</span> <span class="n">get_all_ecuteps</span><span class="p">():</span>
            <span class="n">ec</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">test</span><span class="p">)</span>
            <span class="n">ec_range</span> <span class="o">=</span> <span class="n">tests</span><span class="p">[</span><span class="n">ec</span><span class="p">][</span><span class="s1">&#39;test_range&#39;</span><span class="p">]</span>
            <span class="n">ec_step</span> <span class="o">=</span> <span class="n">ec_range</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">ec_range</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">int</span><span class="p">(</span><span class="n">level</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span> <span class="o">==</span> <span class="n">level</span> <span class="o">/</span> <span class="mi">2</span><span class="p">:</span>
                <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;new ec wedge&#39;</span><span class="p">)</span>
                <span class="c1"># even level of grid extension &gt; new ec wedge</span>
                <span class="n">new_ec_range</span> <span class="o">=</span> <span class="p">(</span><span class="n">ec_range</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="nb">int</span><span class="p">(</span><span class="n">level</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">ec_step</span><span class="p">),)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;new nb wedge&#39;</span><span class="p">)</span>
                <span class="c1"># odd level of grid extension &gt; new nb wedge</span>
                <span class="n">extension</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span>
                    <span class="nb">range</span><span class="p">(</span><span class="n">ec_range</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">ec_step</span><span class="p">,</span> <span class="n">ec_range</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="nb">int</span><span class="p">((</span><span class="n">level</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">))</span> <span class="o">*</span> <span class="n">ec_step</span><span class="p">,</span> <span class="n">ec_step</span><span class="p">))</span>
                <span class="n">new_ec_range</span> <span class="o">=</span> <span class="n">ec_range</span> <span class="o">+</span> <span class="n">extension</span>
            <span class="n">new_tests</span><span class="p">[</span><span class="n">ec</span><span class="p">]</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="s1">&#39;test_range&#39;</span><span class="p">:</span> <span class="n">new_ec_range</span><span class="p">})</span>
        <span class="k">if</span> <span class="n">test</span> <span class="ow">in</span> <span class="n">get_all_nbands</span><span class="p">():</span>
            <span class="n">nb</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">test</span><span class="p">)</span>
            <span class="n">nb_range</span> <span class="o">=</span> <span class="n">tests</span><span class="p">[</span><span class="n">nb</span><span class="p">][</span><span class="s1">&#39;test_range&#39;</span><span class="p">]</span>
            <span class="n">nb_step</span> <span class="o">=</span> <span class="n">nb_range</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">nb_range</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">nb_step</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">int</span><span class="p">(</span><span class="n">level</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span> <span class="o">==</span> <span class="n">level</span> <span class="o">/</span> <span class="mi">2</span><span class="p">:</span>
                <span class="c1"># even level of grid extension &gt; new ec wedge</span>
                <span class="n">extension</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">nb_range</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">nb_step</span><span class="p">,</span> <span class="n">nb_range</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="nb">int</span><span class="p">(</span><span class="n">level</span> <span class="o">/</span> <span class="mi">2</span><span class="p">))</span> <span class="o">*</span> <span class="n">nb_step</span><span class="p">,</span> <span class="n">nb_step</span><span class="p">))</span>
                <span class="n">new_nb_range</span> <span class="o">=</span> <span class="n">nb_range</span> <span class="o">+</span> <span class="n">extension</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># odd level of grid extension &gt; new nb wedge</span>
                <span class="n">new_nb_range</span> <span class="o">=</span> <span class="p">(</span><span class="n">nb_range</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="nb">int</span><span class="p">((</span><span class="n">level</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">nb_step</span><span class="p">),)</span>
            <span class="n">new_tests</span><span class="p">[</span><span class="n">nb</span><span class="p">]</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="s1">&#39;test_range&#39;</span><span class="p">:</span> <span class="n">new_nb_range</span><span class="p">})</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">new_tests</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">new_tests</span></div>


<div class="viewcode-block" id="print_gnuplot_header"><a class="viewcode-back" href="../../../../pymatgen.io.abinit.helpers.html#pymatgen.io.abinit.helpers.print_gnuplot_header">[docs]</a><span class="k">def</span> <span class="nf">print_gnuplot_header</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">title</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;convplot&#39;</span><span class="p">,</span> <span class="n">filetype</span><span class="o">=</span><span class="s1">&#39;jpeg&#39;</span><span class="p">):</span>
    <span class="n">xl</span> <span class="o">=</span> <span class="s1">&#39;set xlabel &quot;nbands&quot;</span><span class="se">\n</span><span class="s1">&#39;</span>
    <span class="n">yl</span> <span class="o">=</span> <span class="s1">&#39;set ylabel &quot;ecuteps (eV)&quot;</span><span class="se">\n</span><span class="s1">&#39;</span>
    <span class="n">zl</span> <span class="o">=</span> <span class="s1">&#39;set zlabel &quot;gap (eV)&quot;</span><span class="se">\n</span><span class="s1">&#39;</span>
    <span class="k">if</span> <span class="n">mode</span> <span class="o">==</span> <span class="s1">&#39;convplot&#39;</span><span class="p">:</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;a&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">filetype</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;set terminal &#39;</span> <span class="o">+</span> <span class="n">filetype</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;set title &quot;&#39;</span> <span class="o">+</span> <span class="n">title</span> <span class="o">+</span> <span class="s1">&#39;&quot;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">xl</span><span class="p">)</span>
        <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">yl</span><span class="p">)</span>
        <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">zl</span><span class="p">)</span>
        <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span></div>


<div class="viewcode-block" id="read_grid_from_file"><a class="viewcode-back" href="../../../../pymatgen.io.abinit.helpers.html#pymatgen.io.abinit.helpers.read_grid_from_file">[docs]</a><span class="k">def</span> <span class="nf">read_grid_from_file</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Read the results of a full set of calculations from file</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;r&#39;</span><span class="p">)</span>
        <span class="n">full_res</span> <span class="o">=</span> <span class="n">ast</span><span class="o">.</span><span class="n">literal_eval</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
        <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="k">except</span> <span class="ne">SyntaxError</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Problems reading &#39;</span><span class="p">,</span> <span class="n">filename</span><span class="p">)</span>
        <span class="n">full_res</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;grid&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;all_done&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">}</span>
    <span class="k">except</span> <span class="p">(</span><span class="ne">OSError</span><span class="p">,</span> <span class="ne">IOError</span><span class="p">):</span>
        <span class="n">full_res</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;grid&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;all_done&#39;</span><span class="p">:</span> <span class="kc">False</span><span class="p">}</span>
    <span class="k">return</span> <span class="n">full_res</span></div>


<div class="viewcode-block" id="is_converged"><a class="viewcode-back" href="../../../../pymatgen.io.abinit.helpers.html#pymatgen.io.abinit.helpers.is_converged">[docs]</a><span class="k">def</span> <span class="nf">is_converged</span><span class="p">(</span><span class="n">hartree_parameters</span><span class="p">,</span> <span class="n">structure</span><span class="p">,</span> <span class="n">return_values</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="n">filename</span> <span class="o">=</span> <span class="n">s_name</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;.conv_res&quot;</span>
    <span class="n">to_return</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;r&#39;</span><span class="p">)</span>
        <span class="n">conv_res</span> <span class="o">=</span> <span class="n">ast</span><span class="o">.</span><span class="n">literal_eval</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
        <span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="n">converged</span> <span class="o">=</span> <span class="kc">True</span> <span class="k">if</span> <span class="kc">True</span> <span class="ow">in</span> <span class="n">conv_res</span><span class="p">[</span><span class="s1">&#39;control&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">()</span> <span class="k">else</span> <span class="kc">False</span>
    <span class="k">except</span> <span class="p">(</span><span class="ne">IOError</span><span class="p">,</span> <span class="ne">OSError</span><span class="p">,</span> <span class="ne">ValueError</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">return_values</span><span class="p">:</span>
            <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Inputfile &#39;</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="s1">&#39; not found, the convergence calculation did not finish properly&#39;</span>
                                          <span class="s1">&#39; or was not parsed ...&#39;</span><span class="p">)</span>
        <span class="n">converged</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="n">converged</span>
    <span class="k">if</span> <span class="n">return_values</span> <span class="ow">and</span> <span class="n">converged</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">hartree_parameters</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">conv_res</span><span class="p">[</span><span class="s1">&#39;values&#39;</span><span class="p">][</span><span class="s1">&#39;ecut&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">conv_res</span><span class="p">[</span><span class="s1">&#39;values&#39;</span><span class="p">][</span><span class="s1">&#39;ecut&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">eV_to_Ha</span> <span class="o">/</span> <span class="mi">4</span><span class="p">)</span>
            <span class="k">except</span> <span class="p">(</span><span class="ne">KeyError</span><span class="p">,</span> <span class="ne">ArithmeticError</span><span class="p">,</span> <span class="ne">FloatingPointError</span><span class="p">,</span> <span class="ne">SyntaxError</span><span class="p">)</span> <span class="k">as</span> <span class="n">ex</span><span class="p">:</span>
                <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;exception in is_converged </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">ex</span><span class="o">.</span><span class="n">message</span><span class="p">)</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">conv_res</span><span class="p">[</span><span class="s1">&#39;values&#39;</span><span class="p">][</span><span class="s1">&#39;ecuteps&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">math</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">conv_res</span><span class="p">[</span><span class="s1">&#39;values&#39;</span><span class="p">][</span><span class="s1">&#39;ecuteps&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="n">eV_to_Ha</span> <span class="o">/</span> <span class="mi">4</span><span class="p">)</span>
            <span class="k">except</span> <span class="p">(</span><span class="ne">KeyError</span><span class="p">,</span> <span class="ne">ArithmeticError</span><span class="p">,</span> <span class="ne">FloatingPointError</span><span class="p">,</span> <span class="ne">SyntaxError</span><span class="p">)</span> <span class="k">as</span> <span class="n">ex</span><span class="p">:</span>
                <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;exception in is_converged </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">ex</span><span class="o">.</span><span class="n">message</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">conv_res</span><span class="p">[</span><span class="s1">&#39;values&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">conv_res</span><span class="p">[</span><span class="s1">&#39;values&#39;</span><span class="p">][</span><span class="n">k</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">conv_res</span><span class="p">[</span><span class="s1">&#39;values&#39;</span><span class="p">][</span><span class="n">k</span><span class="p">]</span> <span class="o">!=</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">:</span>
                <span class="n">to_return</span><span class="o">.</span><span class="n">update</span><span class="p">({</span><span class="n">k</span><span class="p">:</span> <span class="n">conv_res</span><span class="p">[</span><span class="s1">&#39;values&#39;</span><span class="p">][</span><span class="n">k</span><span class="p">]})</span>
        <span class="k">return</span> <span class="n">to_return</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">converged</span></div>


<div class="viewcode-block" id="store_conv_results"><a class="viewcode-back" href="../../../../pymatgen.io.abinit.helpers.html#pymatgen.io.abinit.helpers.store_conv_results">[docs]</a><span class="k">def</span> <span class="nf">store_conv_results</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">folder</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;| Storing results for </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">folder</span><span class="p">):</span>
        <span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="n">folder</span><span class="p">)</span>
    <span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">name</span> <span class="o">+</span> <span class="s1">&#39;.full_res&#39;</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">folder</span><span class="p">,</span> <span class="n">name</span> <span class="o">+</span> <span class="s1">&#39;.full_res&#39;</span><span class="p">))</span>
    <span class="k">for</span> <span class="n">data_file</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;conv_res&#39;</span><span class="p">,</span> <span class="s1">&#39;log&#39;</span><span class="p">,</span> <span class="s1">&#39;conv.log&#39;</span><span class="p">,</span> <span class="s1">&#39;str&#39;</span><span class="p">,</span> <span class="s1">&#39;fitdat&#39;</span><span class="p">,</span> <span class="s1">&#39;convdat&#39;</span><span class="p">,</span> <span class="s1">&#39;data&#39;</span><span class="p">]:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">os</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="n">name</span> <span class="o">+</span> <span class="s1">&#39;.&#39;</span> <span class="o">+</span> <span class="n">data_file</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">folder</span><span class="p">,</span> <span class="n">name</span> <span class="o">+</span> <span class="s1">&#39;.&#39;</span> <span class="o">+</span> <span class="n">data_file</span><span class="p">))</span>
        <span class="k">except</span> <span class="ne">OSError</span><span class="p">:</span>
            <span class="k">pass</span></div>


<div class="viewcode-block" id="add_gg_gap"><a class="viewcode-back" href="../../../../pymatgen.io.abinit.helpers.html#pymatgen.io.abinit.helpers.add_gg_gap">[docs]</a><span class="k">def</span> <span class="nf">add_gg_gap</span><span class="p">(</span><span class="n">structure</span><span class="p">):</span>
    <span class="n">structure</span><span class="o">.</span><span class="n">vbm_l</span> <span class="o">=</span> <span class="s2">&quot;G&quot;</span>
    <span class="n">structure</span><span class="o">.</span><span class="n">cbm_l</span> <span class="o">=</span> <span class="s2">&quot;G&quot;</span>
    <span class="n">structure</span><span class="o">.</span><span class="n">cbm</span> <span class="o">=</span> <span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
    <span class="n">structure</span><span class="o">.</span><span class="n">vbm</span> <span class="o">=</span> <span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">structure</span></div>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.io.abinit.helpers</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>